package lex;

import java.util.Hashtable;

public class RERecogniser {
	
	private Hashtable<Integer, Hashtable<String, Integer>> table;
	private int state;
	
	public RERecogniser() {
		state = 0;
		init();
	}
	
	private void init(){
		table = new Hashtable<Integer, Hashtable<String,Integer>>();
		Hashtable<String, Integer> state1 = new Hashtable<String, Integer>();
		state1.put("a", 1);
		state1.put("b", -1);
		state1.put("c", -1);
		state1.put("d", -1);
		table.put(0, state1);
		
		Hashtable<String, Integer> state2 = new Hashtable<String, Integer>();
		state2.put("a", 3);
		state2.put("b", 2);
		state2.put("c", 2);
		state2.put("d", 2);
		table.put(1, state2);
		
		Hashtable<String, Integer> state3 = new Hashtable<String, Integer>();
		state3.put("a", 3);
		state3.put("b", 2);
		state3.put("c", 2);
		state3.put("d", 2);
		table.put(2, state3);
		
		Hashtable<String, Integer> state4 = new Hashtable<String, Integer>();
		state4.put("a", 1);
		state4.put("b", -1);
		state4.put("c", -1);
		state4.put("d", -1);
		table.put(3, state4);
		
		for(int i = 0; i < 4; i++)System.out.println(table.get(i));
		
	}
	
	public boolean recog(String s){
		return recog(s,0);
	}
	public boolean recog(String s, int pos){
		if(pos==s.length()){
			if(state==0||state==3)return true;
			return false;
		}
		String c = s.substring(pos, pos+1);
		Integer nextState = table.get(state).get(c);
		if(nextState == null||nextState<0)return false;
		else{
			state = nextState;
			return recog(s,(pos+1));
		}

	}
	public static void main(String[] args) {
		RERecogniser re = new RERecogniser();
		System.out.println("The string 'aaabcda' has recog value: " + re.recog("aaabcda"));
		System.out.println("The string 'aaabcda' has recog value: " + re.recog("aaaaaaaaaaaaabcda"));
		System.out.println("The string 'aaabcda' has recog value: " + re.recog("aaabcdbcdbcbdbcbdcdccccdddddaa"));
		
	}
}
